#! /bin/bash

# [Config]
source config

function help() {
    echo "usage: main.sh [<args>]"
    echo "args includes:"
    echo "    start:        Start the Application"
    echo "    stop:         Stop the Application"
    echo "    install:      Install the Application"
    echo "    uninstall:    Uninstall the Application"
    echo "    help:         Get Help"
}

function start() {
    docker start ${SERVER_NAME}
    docker exec -id ${SERVER_NAME} chmod a+x Host/main.py
    docker exec -id ${SERVER_NAME} ./Host/main.py
    docker exec -id ${SERVER_NAME} service apache2 start 

    docker start ${VI_NAME}
    docker exec -id ${VI_NAME} chmod a+x main.py
    docker exec -id ${VI_NAME} ./main.py

    docker start ${LLR_NAME}
    docker exec -id ${LLR_NAME} chmod a+x ./Statistic_Recognition
    docker exec -id ${LLR_NAME} ./Statistic_Recognition/Vehicle_Statistic.py

    if [ "$1" = "-v" ]; then
        docker start ${VM_NAME}
        docker exec -id ${VM_NAME} chmod a+x VideoInterface.py
        docker exec -id ${VM_NAME} ./VideoInterface.py
    fi
}

function stop() {
    if [ "$1" = "-v" ]; then
        docker stop ${VM_NAME}
    fi
    docker stop ${SERVER_NAME}
    docker stop ${VI_NAME}
    docker stop ${LLR_NAME}
}

function restart() {
    stop $1 && start $1
}

function install() {
    if [ "$1" = "-s" ]; then
        install_soft
    elif [ "$1" = "-v" ]; then
        install_vitual_monitor
    else
        docker network create -d bridge ${DOCKER_BRIDGE}
        docker build -t ${SEED_DOCKER_NAME}:${SEED_DOCKER_VERSION} ${SMR_PATH}/Docker/ --rm
        install -s
    fi
}

function install_soft() {
    docker build -t ${SERVER_DOCKER_NAME}:${SERVER_DOCKER_VERSION} ${SMR_PATH}/App/Server/docker/ --rm \
        && docker run -itd -v ${SMR_PATH}/Share:/Share -v ${SMR_PATH}/App/Server/App/Web_Flask:/var/www/html -v ${SMR_PATH}/App/Server/App:/App -p 127.0.0.1:${SERVER_DOCKER_PORT}:80\
            --network ${DOCKER_BRIDGE} \
            --name ${SERVER_NAME} ${SERVER_DOCKER_NAME}:${SERVER_DOCKER_VERSION} \
        && docker start ${SERVER_NAME}

    docker build -t ${VI_DOCKER_NAME}:${VI_DOCKER_VERSION} ${SMR_PATH}/App/Vehicle_Identification/docker/ --rm \
        && docker run -itd -v ${SMR_PATH}/Share:/Share -v ${SMR_PATH}/App/Vehicle_Identification/YOLOv3:/YOLOv3 \
            --network ${DOCKER_BRIDGE} \
            --name ${VI_NAME} ${VI_DOCKER_NAME}:${VI_DOCKER_VERSION} \
        && docker start ${VI_NAME} \
        && docker exec -it ${VI_NAME} make clean \
        && docker exec -it ${VI_NAME} make \
        #&& docker exec -id ${VI_NAME} chmod a+x ./predict
        #&& docker exec -id ${VI_NAME} ./predict

    docker build -t ${LLR_DOCKER_NAME}:${LLR_DOCKER_VERSION} ${SMR_PATH}/App/Lane_Line_Recognition/docker --rm \
        && docker run -itd -v ${SMR_PATH}/Share:/Share -v ${SMR_PATH}/App/Lane_Line_Recognition/App:/App \
            --network ${DOCKER_BRIDGE} \
            --name ${LLR_NAME} ${LLR_DOCKER_NAME}:${LLR_DOCKER_VERSION} \
        && docker start ${LLR_NAME}

        stop && start
}

function install_vitual_monitor() {
    docker build -t ${VM_DOCKER_NAME}:${VM_DOCKER_VERSION} ${SMR_PATH}/App/Vitual_Monitor/docker --rm \
        && docker run -itd -v ${SMR_PATH}/Share:/Share -v ${SMR_PATH}/App/Vitual_Monitor/Interface:/Interface \
            --network ${DOCKER_BRIDGE} \
            --name ${VM_NAME} ${VM_DOCKER_NAME}:${VM_DOCKER_VERSION} \
        && docker start ${VM_NAME}
    
    stop -v && start -v
}

function uninstall() {
    if [ "$1" = "-s" ]; then
        uninstall_soft
    elif [ "$1" = "-v" ]; then
        uninstall_vitual_monitor
    else
        docker network rm ${DOCKER_BRIDGE}
        uninstall_soft
        uninstall_vitual_monitor
        docker image rm ${SEED_DOCKER_NAME}:${SEED_DOCKER_VERSION}
    fi
}

function uninstall_soft() {
    docker stop ${SERVER_NAME}
    docker rm ${SERVER_NAME}
    docker image rm ${SERVER_DOCKER_NAME}:${SERVER_DOCKER_VERSION}

    docker stop ${VI_NAME}
    docker rm ${VI_NAME}
    docker image rm ${VI_DOCKER_NAME}:${VI_DOCKER_VERSION}

    docker stop ${LLR_NAME}
    docker rm ${LLR_NAME}
    docker image rm ${LLR_DOCKER_NAME}:${LLR_DOCKER_VERSION}
}

function uninstall_vitual_monitor() {
    docker stop ${VM_NAME}
    docker rm ${VM_NAME}
    docker image rm ${VM_DOCKER_NAME}:${VM_DOCKER_VERSION}
}

# [Shell]
if [ "$1" = "start" ]; then
    start $2
elif [ "$1" = "install" ]; then
    install $2
elif [ "$1" = "uninstall" ]; then
    uninstall $2
elif [ "$1" = "stop" ]; then
    stop $2
elif [ "$1" = "restart" ]; then
    restart $2
elif [ "$1" = "help" ]; then
    help
else
    echo -e "\033[31mERROR Args\033[0m"
    help
fi
